#!/bin/sh
# SPDX-License-Identifier: MIT
#
# Hotplug script for the Network ports status LuCI application
# (luci-app-tn-netports)
#
# This script version 1.0.4
#
# Place this script to '/etc/hotplug.d/net' folder
#
# Copyright (c) 2019-2020 Tano Systems LLC. All rights reserved.
# Authors: Anton Kikin <a.kikin@tano-systems.com>
#

# Ignore all actions except "add" and "remove"
[ "${ACTION}" != "add" ] &&
[ "${ACTION}" != "remove" ] && exit 0

# INTERFACE must be set
[ -z "${INTERFACE}" ] && exit 0

# Ignore bridges
[ "${DEVTYPE}" = "bridge" ] && exit 0

PORT_DEVICE="${INTERFACE}"

# Replace all '-' to '_' because UCI sections can't contain '-' characters.
PORT_DEVICE_UCI="${PORT_DEVICE//-/_}"

if [ "${ACTION}" = "add" ]; then
	#
	# Ignore interfaces that already exists in the config
	# and which are not hotplug interfaces added by this script.
	#
	if [ `uci -q show luci_netports | \
	      grep -v ".hotplug_" | \
	      grep -c ".ifname='${PORT_DEVICE_UCI}'"` -gt 0 ]; then
		exit 0
	fi

	#
	# Try to make port name
	#
	PORT_NAME=""
	[ -n "${ID_VENDOR}" ] && PORT_NAME="${ID_VENDOR}"

	if [ -n "${ID_MODEL}" ]; then
		if [ -n "${PORT_NAME}" ]; then
			PORT_NAME="${PORT_NAME} ${ID_MODEL}"
		else
			PORT_NAME="${ID_MODEL}"
		fi
	fi

	#
	# Detect type automatically by default
	#
	PORT_TYPE="auto"

	if [ "${PORT_TYPE}" = "auto" ]; then
		#
		# Detect port type by device type
		#
		case "${DEVTYPE}" in
			wireguard)
				PORT_TYPE="vpn"
				[ -z "${PORT_NAME}" ] && PORT_NAME="WireGuard VPN"
				;;
		esac
	fi

	if [ "${PORT_TYPE}" = "auto" ]; then
		#
		# Detect port type by device name
		#
		case "${PORT_DEVICE}" in
			lo|sit*[0-9])
				# Ignore some interfaces
				exit 0
				;;

			eth*[0-9])
				PORT_TYPE="copper"
				[ -z "${PORT_NAME}" ] && PORT_NAME="Ethernet"
				;;

			wlan*[0-9]|ath*[0-9])
				PORT_TYPE="usb_wifi"
				[ -z "${PORT_NAME}" ] && PORT_NAME="Wireless"
				;;

			usb*[0-9])
				PORT_TYPE="usb_rndis"
				[ -z "${PORT_NAME}" ] && PORT_NAME="USB RNDIS"
				;;

			ecm*[0-9])
				PORT_TYPE="usb_rndis"
				[ -z "${PORT_NAME}" ] && PORT_NAME="USB ECM"
				;;

			tun*[0-9]|tap*[0-9])
				PORT_TYPE="vpn"
				[ -z "${PORT_NAME}" ] && PORT_NAME="VPN"
				;;

			wg*[0-9])
				PORT_TYPE="vpn"
				[ -z "${PORT_NAME}" ] && PORT_NAME="WireGuard VPN"
				;;

			ppp*[0-9])
				PORT_TYPE="ppp"
				[ -z "${PORT_NAME}" ] && PORT_NAME="PPP Connection"
				;;

			wwan*[0-9])
				PORT_TYPE="usb_3g"
				[ -z "${PORT_NAME}" ] && PORT_NAME="USB Modem Connection"
				;;

			3g-*)
				PORT_TYPE="gprs"
				[ -z "${PORT_NAME}" ] && PORT_NAME="Modem Connection"
				;;
		esac
	fi

	[ -z "${PORT_NAME}" ] && PORT_NAME="${PORT_DEVICE}"

	#
	# Add or update port information in the configuration
	#
	uci -q set luci_netports.hotplug_${PORT_DEVICE_UCI}=port
	uci -q set luci_netports.hotplug_${PORT_DEVICE_UCI}.name="${PORT_NAME}"
	uci -q set luci_netports.hotplug_${PORT_DEVICE_UCI}.ifname="${PORT_DEVICE}"
	uci -q set luci_netports.hotplug_${PORT_DEVICE_UCI}.type="${PORT_TYPE}"

	#
	# Commit
	#
	uci -q commit luci_netports

elif [ "${ACTION}" = "remove" ]; then

	uci -q delete luci_netports.hotplug_${PORT_DEVICE_UCI}
	uci -q commit luci_netports

fi
